package encrypt

import(
	"encoding/json"
	"errors"

	"gitee.com/lzqysoft/framework/common/aes"
	"gitee.com/lzqysoft/framework/common/stringfmt"
)

//AesEncode json数据aes加密
func  AesEncode(tokenstring string, plantText interface{}) (string, error) {
	key := []byte(stringfmt.Substr(tokenstring, 5, 16))
	jp, _ := json.Marshal(plantText)
	v, err := aes.AesEncrypt(jp, key, key)
	return v, err
}

//AesDecode json数据aes解密返回interface{}
func  AesDecode(cipherstring,tokenstring string) (interface{}, error) {
	if len(cipherstring) < 1 {
		return nil, errors.New("数据为空")
	}
	//	bc := []byte(cipherstring)
	// beego.Debug(cipherstring)
	key := []byte(stringfmt.Substr(tokenstring, 5, 16))
	v, err := aes.AesDecrypt(cipherstring, key, key)
	var s interface{}
	err = json.Unmarshal(v, &s)
	return s, err
}

//AesDecodeToByte json数据aes解密为[]byte
func AesDecodeToByte(cipherstring,tokenstring string) ([]byte, error) {

	//	bc := []byte(cipherstring)
	key := []byte(stringfmt.Substr(tokenstring, 5, 16))
	v, err := aes.AesDecrypt(cipherstring, key, key)

	return v, err
}


